package designPattern.singleton;

/**
 * Author: hu
 * Date: 2021/11/26 08:41
 * 单例模式——懒汉模式
 */
public class Singleton {

    private static Singleton instance;
    private Singleton(){
    }
    //懒汉模式。线程不安全
    public static Singleton getInstance(){
        if(instance==null){
            instance = new Singleton();
        }
        return instance;
    }
    //懒汉模式。线程安全的
    public static synchronized Singleton getInstance1(){
        if(instance==null){
            instance = new Singleton();
        }
        return instance;
    }

    //双重校验锁，线程安全

    public static Singleton getInstance2(){
        if(instance==null){
            synchronized (Singleton.class){
                if (instance==null){
                    instance = new Singleton();
                }
            }

        }
        return instance;

    }

}
